/**
 * @author helio
 */
if(!SALE.dao) SALE.dao = {}
if(!SALE.dao.local) SALE.dao.local = {}
if(!SALE.dao.local.ProductsDao) SALE.dao.local.ProductsDao = function(){
	var productsDoc;
}

SALE.dao.local.ProductsDao.prototype = {	
		/**
		 * 
		 */
		initProductsDoc : function() {
			if(!this.productsDoc) {
				this.productsDoc = LocalXMLFileUtil.getProductsDoc();
			}
			
			return this.productsDoc;
		},
		
		saveProductsDoc : function() {
			LocalXMLFileUtil.saveXML(this.productsDoc,LocalXMLFileUtil.getProductsFile());
		},
		
		insertProductInfo : function(productJson) {
			var root = this.productsDoc.documentElement;
			var maxid = root.getAttribute("maxid");
			if(!maxid) {
				maxid = 0;
			}
			var id = parseInt(maxid) + 1;
			productJson["id"] = id;
			root.setAttribute("maxid",id);
			var productNode = this.productsDoc.createElement("product");
			
			this.updateProduct(productJson, productNode);			
			root.appendChild(productNode);
			
			this.saveProductsDoc();
		},
		
		modifyProductInfo : function(productJson) {
			var productNode = this.searchProductByID(productJson.id);
			
			this.updateProduct(productJson, productNode);
			
			this.saveProductsDoc();
		},
		
		deleteProductInfo : function(id) {
			var productNode = this.searchProductByID(id);
			var root = this.productsDoc.documentElement;
			root.removeChild(productNode);
		},
				
		updateProduct : function(productJosn, productNode) {
			
			for(var attri in productJosn) {	
				//air.trace(attri + ":" + productJosn[attri]);	
				if(attri.indexOf("old") != 0 && productJosn[attri] && productJosn[attri] != "") {
					var attriNodes = Ext.DomQuery.select("/" + attri, productNode);
					if(attriNodes != null && attriNodes.length > 0) {
						productNode.removeChild(attriNodes[0]);
					}
					
					var attriNode = this.productsDoc.createElement(attri);
					var contextNode = this.productsDoc.createCDATASection(productJosn[attri]);
					attriNode.appendChild(contextNode);
					productNode.appendChild(attriNode);
				}
			}
		},
		
		searchProductsElement : function(condition) {
			if(!this.productsDoc) {
				this.initProductsDoc();
			}
			var root = this.productsDoc.documentElement;
			var objArray = Ext.query("product:has(code:first-child)", root);
			var result = [];
		    Ext.each(objArray, function(obj) {
				//alert(Ext.DomQuery.select("/code/*", obj).length);
				var code = Ext.DomQuery.select("/code/*", obj)[0];
				var name = Ext.DomQuery.select("/name/*", obj)[0];
				var abbreviation = Ext.DomQuery.select("/abbreviation/*", obj)[0];
				var model = Ext.DomQuery.select("/model/*", obj)[0];
				var specs = Ext.DomQuery.select("/specs/*", obj)[0];
				var unit = Ext.DomQuery.select("/unit/*", obj)[0];
				var manufacturer = Ext.DomQuery.select("/manufacturer/*", obj)[0];
				var group = Ext.DomQuery.select("/group/*", obj)[0];
				var amount = Ext.DomQuery.select("/amount/*", obj)[0];
				
				var filterFlag = false;
				if(condition.code_from && condition.code_from != "") {
					if(!code || code == null || condition.code_from > code.nodeValue) {
						filterFlag = true;
					}
				}
				
				if(condition.code_to && condition.code_to != "") {
					if(!code || code == null || condition.code_to < code.nodeValue) {
						filterFlag = true;
					}
				}
				
				if(condition.manufacturer && condition.manufacturer != "") {
					if(!manufacturer || manufacturer == null || manufacturer.nodeValue == "" || manufacturer.nodeValue.indexOf(condition.manufacturer) < 0) {
						filterFlag = true;
					}
				}
				
				if(condition.entry_date_from && condition.entry_date_from != "") {
					if(!entry_date || entry_date == null || Date.parse(entry_date.nodeValue) < Date.parse(condition.entry_date_from)) {
						filterFlag = true;
					}
				}
				
				if(condition.entry_date_to && condition.entry_date_to != "") {
					if(!entry_date || entry_date == null || Date.parse(entry_date.nodeValue) > Date.parse(condition.entry_date_to)) {
						filterFlag = true;
					}
				}
				
				if(condition.purchase_date_from && condition.purchase_date_from != "") {
					if(!purchase_date || purchase_date == null || Date.parse(purchase_date.nodeValue) < Date.parse(condition.purchase_date_from)) {
						filterFlag = true;
					}
				}
				
				if(condition.purchase_date_to && condition.purchase_date_to != "") {
					if(!purchase_date || purchase_date == null || Date.parse(purchase_date.nodeValue) > Date.parse(condition.purchase_date_to)) {
						filterFlag = true;
					}
				}
				
				if(condition.status && condition.status != "") {
					
					if((condition.status == '1' || condition.status == 'false' ) && (status && status != null && (status.nodeValue == "true" || status.nodeValue == 1))) {
						filterFlag = true;
					}
					if((condition.status == '2' || condition.status == 'true' ) && (!status || status == null || status.nodeValue == "false" || status.nodeValue == 0)) {
						filterFlag = true;
					}
				}
				
				if(!filterFlag) {
					result.push({
						code : !code || code == null ?  '': code.nodeValue,
						manufacturer : !manufacturer || manufacturer == null ?  '': manufacturer.nodeValue,
						amount : !amount || amount == null ?  '': amount.nodeValue,
						name : !name || name == null ?  '': name.nodeValue,
						model : !model || model == null ?  '': model.nodeValue,
						specs : !specs || specs == null ?  '': specs.nodeValue,
						manufacturer : !manufacturer || manufacturer == null ?  '': manufacturer.nodeValue,
					});
				}

		    });
			return result;
		},
		
		searchProductByID : function(id) {
			if(!this.productsDoc) {
				this.initProductsDoc();
			}
			var root = this.productsDoc.documentElement;
			var objArray = Ext.query("/product:has(id:nodeValue(" + id+ "))", root);
			var result;
			if(objArray.length == 1) {
				result = objArray[0];
			}
			
			return result;
		},
		
		searchProductJsonByID : function(id) {
			
			var resultNode = this.searchProductByID(id);
			
			var result = {};
			
			if (resultNode != null) {
				var children = resultNode.childNodes;
				for (var i = 0; i < children.length; i++) {
					var child = children[i];					
					if (child.nodeType == 1) {
						if (!result[child.nodeName] && child.childNodes[0] != null) {
							result[child.nodeName] = child.childNodes[0].nodeValue;
						}
					}
				}
			}
			return result;
		},
		
		buildProductsJson : function() {
			var root = this.initProductsDoc().documentElement;
			if(root != null) {
				var objArray = Ext.query("/product", root);
				var productsJson = {products: []};
				
				Ext.each(objArray, function(product) {
					var children = product.childNodes;
					var productJson = {};
					for(var i = 0; i < children.length; i++) {
						var child = children[i];
						
						if(child.nodeType == 1) {
							if (!productJson[child.nodeName] && child.childNodes[0] != null) {
								productJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}			
					}
					
					productsJson.products.push(productJson);
				});
				
				return productsJson;
			}
		}	
}